﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Windows.Forms;

namespace MainApp
{
    public partial class FrmInputSells : DevExpress.XtraEditors.XtraForm
    {
        //checked
        private void LoadGroupProducts()
        {
            Guid productId;
            string productName;
            int i = data.DsOil.Products.Rows.Count + 3;
            foreach (DataRow row in data.DsOil.Products.Rows)
            {
                productId = (Guid)row["ID"];
                productName = (string)row["ProductName"];

                //DevExpress.XtraEditors.GroupControl tmp = new DevExpress.XtraEditors.GroupControl { AutoSize = true, AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink, Dock = System.Windows.Forms.DockStyle.Left, Location = new System.Drawing.Point(446, 22), MinimumSize = new System.Drawing.Size(111, 64), Size = new System.Drawing.Size(111, 66), Name = "group" + productId.ToString(), Text = productName, TabIndex = i-- };                
                DevExpress.XtraEditors.GroupControl tmp = new DevExpress.XtraEditors.GroupControl { AutoSize = true, AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink, Dock = System.Windows.Forms.DockStyle.Right, Location = new System.Drawing.Point(446, 22), MinimumSize = new System.Drawing.Size(111, 64), Size = new System.Drawing.Size(111, 66), Name = "group" + productId.ToString(), Text = productName, TabIndex = i-- };                
                tmp.AppearanceCaption.Font = new System.Drawing.Font("Tahoma", (float)8.25, System.Drawing.FontStyle.Bold);

                DevExpress.XtraEditors.SpinEdit spin = new DevExpress.XtraEditors.SpinEdit { Name = "spinReal" + productId.ToString(), Dock = System.Windows.Forms.DockStyle.Top };
                spin.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                spin.Properties.DisplayFormat.FormatString = "n2";
                spin.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                spin.Properties.EditFormat.FormatString = "n2";
                //spin.Properties.Appearance.Font = new System.Drawing.Font("Tahoma", (float)8.25, System.Drawing.FontStyle.Bold);
                spin.Properties.Buttons.Clear();
                tmp.Controls.Add(spin);
                productsSpinReal.Add(productId, spin);

                spin = new DevExpress.XtraEditors.SpinEdit { Name = "spinCalc" + productId.ToString(), Dock = System.Windows.Forms.DockStyle.Top, TabStop = false };
                spin.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                spin.Properties.DisplayFormat.FormatString = "n2";
                spin.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                spin.Properties.EditFormat.FormatString = "n2";
                spin.Properties.ReadOnly = true;
                spin.Properties.Buttons.Clear();
                //spin.Properties.Appearance.Font = new System.Drawing.Font("Tahoma", (float)8.25, System.Drawing.FontStyle.Bold);
                tmp.Controls.Add(spin);
                productsSpinCalc.Add(productId, spin);
                spin.EditValueChanged += new EventHandler(spinCalc_EditValueChanged);

                groupControlProducts.Controls.Add(tmp);
            }

            //draw help
            DevExpress.XtraEditors.GroupControl temp = new DevExpress.XtraEditors.GroupControl { AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink, Dock = System.Windows.Forms.DockStyle.Left, Location = new System.Drawing.Point(2, 22), MinimumSize = new System.Drawing.Size(120, 64), Name = "groupControlHelp", Size = new System.Drawing.Size(111, 66), TabIndex = 6, Text = "Loại" };
            temp.AppearanceCaption.Font = new System.Drawing.Font("Tahoma", (float)8.25, System.Drawing.FontStyle.Bold);
            // textEdit1
            DevExpress.XtraEditors.TextEdit text = new DevExpress.XtraEditors.TextEdit { Dock = System.Windows.Forms.DockStyle.Top, EditValue = "Chỉ số thực tế (lít)", Location = new System.Drawing.Point(2, 42), Name = "txtTotalReal", TabStop = false };
            text.Properties.AllowFocused = false;
            text.Properties.ReadOnly = true;
            text.Font = new System.Drawing.Font("Tahoma", (float)8.25, System.Drawing.FontStyle.Bold);
            temp.Controls.Add(text);
            // textEdit2
            text = new DevExpress.XtraEditors.TextEdit { Dock = System.Windows.Forms.DockStyle.Top, EditValue = "Chỉ số tính toán (lít)", Location = new System.Drawing.Point(2, 22), Name = "txtTotalCalc", TabStop = false };
            text.Properties.AllowFocused = false;
            text.Properties.ReadOnly = true;
            text.Font = new System.Drawing.Font("Tahoma", (float)8.25, System.Drawing.FontStyle.Bold);
            temp.Controls.Add(text);
            groupControlProducts.Controls.Add(temp);
        }

        private void spinCalc_EditValueChanged(object sender, EventArgs e)
        {
            DevExpress.XtraEditors.SpinEdit spin = sender as DevExpress.XtraEditors.SpinEdit;
            string spinName = spin.Name.Substring(8);
            spinName = "spinReal" + spinName;
            ((DevExpress.XtraEditors.SpinEdit)spin.Parent.Controls[spinName]).Properties.MinValue = (decimal)spin.EditValue;
        }
        //checked
        private decimal GetTotalImport(Guid storeId, Guid productId)
        {
            try
            {
                return (decimal)data.DsOil.ImportStatisticsAll.FindByStoreIDProduct(storeId, productId)["TotalAmount"];
            }
            catch (Exception)
            {
                return 0;
                //throw;
            }
        }
        //checked
        private decimal GetTotalExport(Guid storeId, Guid productId)
        {
            try
            {
                return (decimal)data.DsOil.ExportStatisticsAll.FindByProductStoreID(productId, storeId)["TotalAmount"];
            }
            catch (Exception)
            {
                return 0;
                //throw;
            }
        }
        //checked
        private bool ValidateRetail(DataRow row)
        {
            if ((Guid)row["Customer"] == Guid.Empty)
            {
                decimal paid = (decimal)row["Paid"];
                decimal total = (decimal)row["Amount"] * (decimal)row["Price"];
                if (paid < total)
                    return false;
                else
                    return true;
            }
            else
                return true;
        }
        //checked
        private void CopyASell(OilDB.DataSetOilManagement.SellsRow row, OilDB.DataSetOilManagement.SellsRow newRow)
        {
            newRow.ID = row.ID;
            newRow.StoreID = row.StoreID;
            newRow.SellTime = row.SellTime;
            newRow.Product = row.Product;
            newRow.Amount = row.Amount;
            newRow.Price = row.Price;
            newRow.Commission = row.Commission;
            newRow.Paid = row.Paid;
            newRow.Note = row.Note;
            newRow.Customer = row.Customer;
        }
        //checked
        private void CalcDebt(OilDB.DataSetOilManagement.SellsRow row)
        {
            Guid customerId = row.Customer;
            if (customerId != Guid.Empty)
                if (row.Total > row.Paid)
                {
                    OilDB.DataSetOilManagement.DebtStatistics_AllRow debtRow = data.DsOil.DebtStatistics_All.FindByCustomer(customerId);
                    if (debtRow == null)
                    {
                        debtRow = data.DsOil.DebtStatistics_All.NewDebtStatistics_AllRow();
                        debtRow.Customer = row.Customer;
                        debtRow.Company = (Guid)data.DsOil.Customers.FindByID(debtRow.Customer)["Company"];
                        debtRow.Debt = row.Total - row.Paid;
                        data.DsOil.DebtStatistics_All.AddDebtStatistics_AllRow(debtRow);
                    }
                    else
                        debtRow.Debt += row.Total - row.Paid;
                }
        }

        private void InitControlProperties()
        {
            cboStores.EditValue = currentStore;
            cboStores.Properties.ReadOnly = true;
            colCommission.Visible = false;
        }
    }
}
